﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using Pyramid.Bll;
using Pyramid.Models;

namespace Pyramid.Controllers
{
	public class StaffController : BaseController
	{
		private readonly StaffHelper _helper = new StaffHelper();
		private readonly SelectListHelper _listHelper = new SelectListHelper();

		#region staff base info

		public ActionResult List(StaffQueryModel model)
		{
			return View(PrepareForListView(model));
		}

		public ActionResult Create()
		{
			var model = new StaffEditViewModel
			{
				Staff = new Staff
				{
					Birthday = DateTime.Parse("1982-01-01"),
					EntryDate = DateTime.Now,
					CreateUserId = User.UserId,
					CreateTime = DateTime.Now,
					EditUserId = User.UserId,
					EditTime = DateTime.Now
				},
				Departments = _listHelper.Departments(),
				StaffPositions = _listHelper.StaffPositions()
			};
			return View("Edit", model);
		}

		[HttpGet]
		public ActionResult Edit(int id)
		{
			var model = new StaffEditViewModel
			{
				Staff = _helper.GetStaff(id)
			};
			model.Departments = _listHelper.Departments(model.Staff.DepartmentId);
			model.StaffPositions = _listHelper.StaffPositions(model.Staff.PositionId);
			return View(model);
		}

		[HttpPost]
		public ActionResult Edit(StaffEditViewModel model)
		{
			if (Db.Staves.Any(x => x.Id != model.Staff.Id && x.Code == model.Staff.Code))
			{
				return JError("工号已被占用，请修改！");
			}

			var staff = model.Staff;
			if (staff.Id == 0)
			{
				staff.CreateUserId = User.UserId;
				staff.CreateTime = DateTime.Now;
				staff.EditUserId = User.UserId;
				staff.EditTime = DateTime.Now;
				Db.Staves.Add(staff);
			}
			else
			{
				staff.EditUserId = User.UserId;
				staff.EditTime = DateTime.Now;
				Db.Entry(staff).State = EntityState.Modified;
			}
			Db.SaveChanges();

			return JSuccess("操作成功！", "tab_bsi_staff");
		}

		public ActionResult Delete(int id)
		{
			var model = new Staff {Id = id};
			Db.Entry(model).State = EntityState.Deleted;
			Db.SaveChanges();
			return JSuccess("删除成功！", "tab_bsi_staff");
		}

		#endregion

		#region staff account

		public ActionResult AccountList(StaffQueryModel model)
		{
			return View(PrepareForListView(model));
		}

		public ActionResult SetAccount(int staffId)
		{
			var staff = _helper.GetStaff(staffId);
			ViewBag.Account = string.IsNullOrWhiteSpace(staff.UserId) ?
				new Account {Id = 0, ExtInfoId = staff.Id, ExtInfoName = staff.FullName, UserType = (int) UserTypes.Staff, FullName = staff.FullName}
				: Db.Accounts.Include(x => x.Roles).First(x => x.UserId == staff.UserId);

			return View(staff);
		}

		[HttpPost]
		public ActionResult SetAccount(int staffId, Account model, int[] selectedRoleIds)
		{
			var helper = AccountHelper.NewInstance;
			return helper.InsertOrUpdate4Staff(staffId, model, selectedRoleIds) ? JSuccess("账号设置成功！", "tab_bsi_staffaccount") : JError(helper.Message);
		}

		#endregion

		protected List<Staff> PrepareForListView(StaffQueryModel model)
		{
			var staves = _helper.GetStaves(model);

			var currentPage = (model.PageNum ?? 1) - 1;
			var pageSize = model.NumPerPage ?? 20;

			var vm = new StaffListViewModel
			{
				Departments = _listHelper.Departments(model.DepartmentId, true),
				CurrentPageIndex = currentPage,
				PageSize = pageSize,
				TotalCount = staves.Count(),
				StaffPositions = _listHelper.StaffPositions(model.PositionId, true),
				QueryModel = model
			};
			ViewBag.Bag = vm;

			return staves.OrderBy(x => x.CreateTime).Skip(currentPage*pageSize).Take(pageSize).ToList();
		}
	}
}